/**
 * @file
 * @auth zhaojing
 * @date 2016/11/24
 */

import './style.scss';
import tpl from './tpl.html';

import Vue from 'vue';
import modal from '../../../../../grsc-base/component/Modal.vue';
import help from '../../../../../grsc-base/component/Help.vue';
import Select from '../../../../../grsc-base/component/Select.vue';
import Popover from '../../../../../grsc-base/component/Popover.vue';
import Validator from '../../../../../grsc-base/component/Validator/main';
import VerifyCode from '../../../../component/VerifyCode.vue';
import {REG_EMAIL, REG_PHONE, REG_PAYPWD, REG_ID} from '../../../../../grsc-base/common/constants';
import {RULE_ASSET_PASSWORD_LENGTH, RULE_ASSET_PASSWORD_PATTERN, RULE_CODE_SEND, REG_PASSPORT} from '../../../../common/contants';
import lib from '../../../../common/lib';

import service from './service';

export default  Vue.extend({
    template: tpl,
    props: {
        onBindingSuccess: {
            type: Function,
            default() {
            }
        }
    },
    components: {
        modal,
        help,
        Popover,
        VerifyCode,
        Validator,
        VSelect: Select
    },
    created() {
        this.queryGoogleStatus()
    },
    mounted() {

    },
    watch: {},
    computed: {},
    data() {
        return {
            selectParams: {
                options: lib.getIdTypeList(this.$i18n.locale)
            },
            idForm: {
                cardType: 'PASSPORT',
                cardNo: '',
                identifyingCode: ''
            },
            pwdForm:{
                password: '',
                confirmPwd: ''
            },
            idRules:{
                cardNo: {
                    required: true,
                    requiredTip: this.$t('basic.securitySetting.IDTip'),
                    minlength: 3,
                    minlengthTip: this.$t('basic.securitySetting.ruleDocNumber'),
                    maxlength: 50,
                    maxlengthTip: this.$t('basic.securitySetting.ruleDocNumber'),
                    pattern: REG_PASSPORT,
                    patternTip: this.$t('basic.securitySetting.IncorrectIdNumTip')
                },
                identifyingCode: {
                    required: true,
                    requiredTip: this.$t('common.verifyCodeRequireTip'),
                    pattern: '\\d{6}',
                    patternTip: window.CODE.IDENTIFY[this.$i18n.locale]
                }
            },
            pwdRules:{
                password: {
                    tip:'',
                    required: true,
                    requiredTip: this.$t('common.newPassRequireTip'),
                    minlength: 8,
                    minlengthTip: this.$t('common.ruleAssetPassLength'),
                    maxlength: 20,
                    maxlengthTip: this.$t('common.ruleAssetPassLength'),
                    pattern: REG_PAYPWD,
                    patternTip: this.$t('common.ruleAssetPassPattern')
                },
                confirmPwd: {
                    required: true,
                    requiredTip: this.$t('common.repeatPassRequireTip'),
                    equalTo: 'password',
                    equalToTip: this.$t('common.passEqualTip')
                }
            },
            isDisabled: true,
            hasSubmitted: false,
            isRequesting: false,
            resetBefore: true,
            googleStatus:'OFF'
        }
    },
    methods: {
        setSelectVal(val){
            this.idForm.cardType = val;
        },
        onSendSuccess() {
            this.idRules.identifyingCode.$setRight(this.$t('common.codeSentTip'));
        },
        onSendFail(rep) {
            this.idRules.identifyingCode.$setError(rep.msg);
        },
        resetPayPwdBefore(){
            if(this.idRules.$checkAll()===false){
                return;
            }
            this.resetBefore = false;
        },


        queryGoogleStatus(){
            let googleBound = window.localStorage.getItem('GOOGLE_BOUND');
            let googleStatus = window.localStorage.getItem('GOOGLE_STATUS');

            if(googleBound === 'true' && googleStatus === 'ON'){
                this.googleStatus = googleStatus;
                this.idForm.googleCode = '';
                this.idRules.googleCode = {
                    required: true,
                    requiredTip: this.$t('common.googleCodeRequireTip'),
                    pattern: '\\d{6}',
                    patternTip: window.CODE.GOOGLE_CODE[this.$i18n.locale]
                };
            }else{
                this.googleStatus = 'OFF';
            }
        },


        submit(){
            if(this.pwdRules.$checkAll()===false){
                return;
            }

            this.isRequesting = true;

            let param = {
                payPassword: this.pwdForm.password
            };

            let header = {
                'service-code': this.idForm.identifyingCode,
                'passport-no': this.idForm.cardNo.toUpperCase()
            };
            if(this.googleStatus === 'ON'){
                header['google-code'] = this.idForm.googleCode
            }

            service.resetPayPwd(param, header).done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                    this.hide();
                    window.success(this.$t('common.success'));
                } else {
                    if (rep.code === window.CODE.PAY_PWD_FORMAT.CODE) {
                        this.pwdRules.password.$setError(window.CODE.PAY_PWD_FORMAT[this.$i18n.locale]);
                    } else if (rep.code === window.CODE.IDENTIFY_INVALID.CODE) {
                        this.resetBefore = true;
                        setTimeout(() => {
                            this.idRules.identifyingCode.$setError(window.CODE.IDENTIFY_INVALID[this.$i18n.locale]);
                        }, 0);
                    } else if(rep.code === window.CODE.IDENTIFY.CODE){
                        this.resetBefore = true;
                        // this.pwdForm.password = '';
                        // this.pwdForm.confirmPwd = '';
                        setTimeout(() => {
                            this.idRules.identifyingCode.$setError(window.CODE.IDENTIFY[this.$i18n.locale]);
                        }, 0);
                    } else if (rep.code === window.CODE.NO_AUTH.CODE) {
                        this.resetBefore = true;
                        setTimeout(() => {
                            window.error(this.$t('basic.securitySetting.securityTip'));
                        }, 0);
                    } else if (rep.code === window.CODE.AUTH_ERROR.CODE) {
                        this.resetBefore = true;
                        setTimeout(() => {
                            this.idRules.cardNo.$setError(this.$t('basic.securitySetting.IncorrectIdNumTip'));
                        }, 0);
                    } else if (rep.code === window.CODE.ID_CARD_ERROR.CODE) {
                        this.resetBefore = true;
                        setTimeout(() => {
                            this.idRules.cardNo.$setError(window.CODE.ID_CARD_ERROR[this.$i18n.locale]);
                        }, 0);
                    } else if(rep.code === window.CODE.GOOGLE_CODE.CODE){
                        this.resetBefore = true;
                        setTimeout(() => {
                            this.idRules.googleCode.$setError(window.CODE.GOOGLE_CODE[this.$i18n.locale]);
                        }, 0);
                    } else {
                        window.error(rep.msg);
                    }
                }
            }).always(()=> {
                this.isRequesting = false;
            });
        },
        focusTips(){
            this.pwdRules.password.tip = this.$t('common.ruleAssetPassLength');
        },
        blurTips(){
            this.pwdRules.password.tip = "";
        },
        hide() {
            this.$emit('showResetPayPwd');
        }
    }

});
